<?php
/**
 * 系統名稱: Lotto 系統
 * 檔案說明: 專案管理-修改
 * $Author$
 * $Id$
 *
 */
set_time_limit(0);
$arrToday = split("-", date("Y-m-d"));
$strMaxDate = date("Y-m-d", mktime(0, 0, 0, $arrToday[1] - 2, 1, $arrToday[0]));

//include("AllWeb.inc.php");
//foreach($arrWeb as $strWeb){
	$strWeb = "us11.us";
	print $strWeb.$_SERVER["DOCUMENT_ROOT"]."<br>\n";
	$_SERVER["DOCUMENT_ROOT"] = "/home/share/www/".$strWeb."/secure";
	include($_SERVER["DOCUMENT_ROOT"]."/db.inc.php");


	$strSQL = "select Max(ID) as MaxID, Min(ID) as MinID from `bet_BetList` where `PayupDate` < '".$strMaxDate."'";
	if(!$RS = PMA_mysql_query($strSQL, $_ConnMaster)){
		print "Find Data Error!\n";
		print "SQL: ".$strSQL.", \nError: ".PMA_mysql_error($_ConnMaster).", \nLine: ".__LINE__.", \nPage: ".__FILE__."\n";
		break;
	}
	print $strSQL;
	$Rows = PMA_mysql_fetch_object($RS);
	$intMaxID = $Rows -> MaxID;
	$intMinID = $Rows -> MinID;

	PMA_mysql_query("BEGIN", $_ConnMaster);
	//資料轉移
	$strSQL = "insert into `bet_OldBetList`(`ID`, `LottoID`, `Bet_Type`, `GambleID`, `Number`, `OddsID`, `OddsID2`, `Odds`, `Odds2`, `MemID`, `MemExchangeRate`, `MemCurrency`, `MemStake`, `MemPayOff`, `MemResult`, `MemCommission`, `SubID`, `SubIncome`, `SubCommission`,  `SubResult`, `SubRate`, `SubRateResult`, `MainID`, `MainCommission`, `MainResult`, `MainRate`, `MainRateResult`, `StockID`, `StockCommission`, `StockResult`, `StockRate`, `StockRateResult`, `PayupDate`, `isPayOff`, `WagerType`) select `ID`, `LottoID`, `Bet_Type`, `GambleID`, `Number`, `OddsID`, `OddsID2`, `Odds`, `Odds2`, `MemID`, `MemExchangeRate`, `MemCurrency`, `MemStake`, `MemPayOff`, `MemResult`, `MemCommission`, `SubID`, `SubIncome`, `SubCommission`,  `SubResult`, `SubRate`, `SubRateResult`, `MainID`, `MainCommission`, `MainResult`, `MainRate`, `MainRateResult`, `StockID`, `StockCommission`, `StockResult`, `StockRate`, `StockRateResult`, `PayupDate`, `isPayOff`, `WagerType` from `bet_BetList` where ID >= ".$intMinID." and ID <= ".$intMaxID;
	if(!PMA_mysql_query($strSQL, $_ConnMaster)){
		print "Add Data Error!\n";
		print "SQL: ".$strSQL.", \nError: ".PMA_mysql_error($_ConnMaster).", \nLine: ".__LINE__.", \nPage: ".__FILE__."\n";
		PMA_mysql_query("ROLLBACK", $_ConnMaster);
		break;
	}

	$strSQL = "insert into `bet_OldBetListContent`(`ID`, `GameDate`, `BetDate`, `BetTime`, `Content`, `checkkey`, `GambleType`, `MemAccount`, `isBlack`, `NNWin`, `NSWin`, `JoinNumber`, `OneStake`, `SuppleID`) select `ID`, `GameDate`, `BetDate`, `BetTime`, `Content`, `checkkey`, `GambleType`, `MemAccount`, `isBlack`, `NNWin`, `NSWin`, `JoinNumber`, `OneStake`, `SuppleID` from `bet_BetListContent` where ID >= ".$intMinID." and ID <= ".$intMaxID;
	if(!PMA_mysql_query($strSQL, $_ConnMaster)){
		print "Add Data Error!\n";
		print "SQL: ".$strSQL.", \nError: ".PMA_mysql_error($_ConnMaster).", \nLine: ".__LINE__.", \nPage: ".__FILE__."\n";
		PMA_mysql_query("ROLLBACK", $_ConnMaster);
		break;
	}
	PMA_mysql_query("COMMIT", $_ConnMaster);
	//資料比較 BetList
	$strSQL = "select COUNT(*) as tolNo, SUM(MemStake) as tolMemStake, SUM(MemPayOff) as tolMemPayOff, SUM(MemResult) as tolMemResult, SUM(Bet_Type) as tolBet_Type, SUM(MemCommission) as tolMemCommission, SUM(SubIncome) as tolSubIncome, SUM(SubCommission) as tolSubCommission, SUM(SubResult) as tolSubResult, SUM(SubRate) as tolSubRate, SUM(SubRateResult) as tolSubRateResult, SUM(MemID) as tolMemID, SUM(SubID) as tolSubID, SUM(MainCommission) as tolMainCommission, SUM(MainResult) as tolMainResult, SUM(MainRate) as tolMainRate, SUM(StockRateResult) as tolStockRateResult from `bet_BetList` where ID >= ".$intMinID." and ID <= ".$intMaxID;
	if(!$RS = PMA_mysql_query($strSQL, $_ConnMaster)){
		print "Find Data Error!\n";
		print "SQL: ".$strSQL.", \nError: ".PMA_mysql_error($_ConnMaster).", \nLine: ".__LINE__.", \nPage: ".__FILE__."\n";
		PMA_mysql_query("ROLLBACK", $_ConnMaster);
		break;
	}

	$Rows = PMA_mysql_fetch_object($RS);
	$arrR[tolNo] = $Rows -> tolNo;
	$arrR[tolMemStake] = $Rows -> tolMemStake;
	$arrR[tolMemPayOff] = $Rows -> tolMemPayOff;
	$arrR[tolBet_Type] = $Rows -> tolBet_Type;
	$arrR[tolMemCommission] = $Rows -> tolMemCommission;
	$arrR[tolSubIncome] = $Rows -> tolSubIncome;
	$arrR[tolSubCommission] = $Rows -> tolSubCommission;
	$arrR[tolSubResult] = $Rows -> tolSubResult;
	$arrR[tolSubRate] = $Rows -> tolSubRate;
	$arrR[tolSubRateResult] = $Rows -> tolSubRateResult;
	$arrR[tolMemID] = $Rows -> tolMemID;
	$arrR[tolSubID] = $Rows -> tolSubID;
	$arrR[tolMainCommission] = $Rows -> tolMainCommission;
	$arrR[tolMainResult] = $Rows -> tolMainResult;
	$arrR[tolMainRate] = $Rows -> tolMainRate;
	$arrR[tolStockRateResult] = $Rows -> tolStockRateResult;

	$strSQL = "select COUNT(*) as tolNo, SUM(MemStake) as tolMemStake, SUM(MemPayOff) as tolMemPayOff, SUM(MemResult) as tolMemResult, SUM(Bet_Type) as tolBet_Type, SUM(MemCommission) as tolMemCommission, SUM(SubIncome) as tolSubIncome, SUM(SubCommission) as tolSubCommission, SUM(SubResult) as tolSubResult, SUM(SubRate) as tolSubRate, SUM(SubRateResult) as tolSubRateResult, SUM(MemID) as tolMemID, SUM(MemID) as tolMemID, SUM(MainCommission) as tolMainCommission, SUM(MainResult) as tolMainResult, SUM(MainRate) as tolMainRate, SUM(StockRateResult) as tolStockRateResult from `bet_OldBetList` where ID >= ".$intMinID." and ID <= ".$intMaxID;
	if(!$RS = PMA_mysql_query($strSQL, $_ConnMaster)){
		print "Find Data Error!\n";
		print "SQL: ".$strSQL.", \nError: ".PMA_mysql_error($_ConnMaster).", \nLine: ".__LINE__.", \nPage: ".__FILE__."\n";
		//PMA_mysql_query("ROLLBACK", $_ConnMaster);
		break;
	}

	$Rows = PMA_mysql_fetch_array($RS, MYSQL_ASSOC);
	foreach($Rows as $k => $v){
		if(round($arrR[$k]*100000) != round($v*100000)){
			print "BetList => ".$k."不對 ".$arrR[$k]." != ".$v." \n";
			//PMA_mysql_query("ROLLBACK", $_ConnMaster);
			break;
		}
	}

	//資料比較 BetListContent
	$strSQL = "select COUNT(*) as tolNo, SUM(OneStake) as tolOneStake, SUM(NNWin) as tolNNWin, SUM(NSWin) as tolNSWin from `bet_BetListContent` where ID >= ".$intMinID." and ID <= ".$intMaxID;
	if(!$RS = PMA_mysql_query($strSQL, $_ConnMaster)){
		print "Find Data Error!\n";
		print "SQL: ".$strSQL.", \nError: ".PMA_mysql_error($_ConnMaster).", \nLine: ".__LINE__.", \nPage: ".__FILE__."\n";
		//PMA_mysql_query("ROLLBACK", $_ConnMaster);
		break;
	}
	$Rows = PMA_mysql_fetch_object($RS);
	$tolNo = $Rows -> tolNo;
	$tolOneStake = $Rows -> tolOneStake;
	$tolNNWin = $Rows -> tolNNWin;
	$tolNSWin = $Rows -> tolNSWin;

	$strSQL = "select COUNT(*) as tolNo, SUM(OneStake) as tolOneStake, SUM(NNWin) as tolNNWin, SUM(NSWin) as tolNSWin from `bet_OldBetListContent` where ID >= ".$intMinID." and ID <= ".$intMaxID;
	if(!$RS = PMA_mysql_query($strSQL, $_ConnMaster)){
		print "Find Data Error!\n";
		print "SQL: ".$strSQL.", \nError: ".PMA_mysql_error($_ConnMaster).", \nLine: ".__LINE__.", \nPage: ".__FILE__."\n";
		//PMA_mysql_query("ROLLBACK", $_ConnMaster);
		break;
	}

	$Rows = PMA_mysql_fetch_object($RS);
	if($tolNo != $Rows -> tolNo){
		print "BetListContent 總比數不對 ".$tolNo." != ".$Rows -> tolNo." \n";
		//PMA_mysql_query("ROLLBACK", $_ConnMaster);
		break;
	}
	if($tolOneStake != $Rows -> tolOneStake){
		print "BetListContent 總注額不對 ".$tolOneStake." != ".$Rows -> tolOneStake." \n";
		//PMA_mysql_query("ROLLBACK", $_ConnMaster);
		break;
	}
	if($tolNNWin != $Rows -> tolNNWin){
		print "BetListContent 總派彩不對 ".$tolNNWin." != ".$Rows -> tolNNWin." \n";
		//PMA_mysql_query("ROLLBACK", $_ConnMaster);
		break;
	}
	if($tolNSWin != $Rows -> tolNSWin){
		print "BetListContent 總結果不對 ".$tolNSWin." != ".$Rows -> tolNSWin." \n";
		//PMA_mysql_query("ROLLBACK", $_ConnMaster);
		break;
	}
	$strSQL = "delete from `bet_BetList` where ID >= ".$intMinID." and ID <= ".$intMaxID;
	print $strSQL."\n";
/*
	if(!PMA_mysql_query($strSQL, $_ConnMaster)){
		print "Del Data Error!\n";
		print "SQL: ".$strSQL.", \nError: ".PMA_mysql_error($_ConnMaster).", \nLine: ".__LINE__.", \nPage: ".__FILE__."\n";
		PMA_mysql_query("ROLLBACK", $_ConnMaster);
		break;
	}
	$strSQL = "delete from `bet_BetListContent` where ID >= ".$intMinID." and ID <= ".$intMaxID;
	if(!PMA_mysql_query($strSQL, $_ConnMaster)){
		print "Del Data Error!\n";
		print "SQL: ".$strSQL.", \nError: ".PMA_mysql_error($_ConnMaster).", \nLine: ".__LINE__.", \nPage: ".__FILE__."\n";
		PMA_mysql_query("ROLLBACK", $_ConnMaster);
		break;
	}
	PMA_mysql_query("COMMIT", $_ConnMaster);
*/
	print "筆數：".$tolNo;
	print ", MaxID：".$intMaxID;
	print ", MinID：".$intMinID;
	print ", MaxDate：".$strMaxDate;
	print ", 日期：".date("Y-m-d H:i:s")."<br>\n<hr>\n<br>\n";
//} //foreach($arrWeb as $strWeb){
?>